package cn.easy.kitchen.data;

import cn.easy.base.utils.PoiUtils;
import cn.easy.kitchen.domain.*;
import cn.easy.kitchen.service.*;
import cn.easy.kitchen.utils.ProjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
public class ImportFromExcel {
    private PathMatchingResourcePatternResolver loader = new PathMatchingResourcePatternResolver();
    @Autowired
    MaterialBaseService materialBaseService;
    @Autowired
    BomService bomService;
    @Autowired
    CustomerService customerService;
    @Autowired
    CustomerTransGroupService customerTransGroupService;
    @Autowired
    NutritionService nutritionService;
    @Autowired
    NutritionTypeService nutritionTypeService;

    public void importBom() {
        Resource resource = loadResource("bom-总结.xlsx");
        try {

            File file = resource.getFile();
            String[][] data = PoiUtils.getFromExcel(13, file);
            List<Bom> bomList = new ArrayList<>();
            if (data != null && data.length > 0) {
                bomService.deleteAll();
                for (int i = 1; i < data.length; i++) {
                    String[] d = data[i];
                    Bom bom = new Bom();
                    bom.setNo(d[0]);
                    MaterialBase destMaterBase = materialBaseService.findByNo(d[1]);
                    if (destMaterBase != null) {
                        bom.setDestMaterialId(destMaterBase.getId());
                    }
                    MaterialBase srcMaterBase = materialBaseService.findByNo(d[3]);
                    if (srcMaterBase != null) {
                        bom.setSrcMaterialId(srcMaterBase.getId());
                    }
                    bom.setBomLevel(d[5]);
                    bom.setSrcMaterialWeight(ProjectUtil.getBigDecimalVal(d[8]).multiply(ProjectUtil.getBigDecimalVal(d[10])));
                    bomList.add(bom);
                }
                bomService.save(bomList);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private String getCustomerId(String customerType) {
        String customerId = "";
        switch (customerType) {
            case "1":
                customerId = "00000000000000000000000000000001";
                break;
            case "2":
                customerId = "00000000000000000000000000000002";
                break;
            case "3":
                customerId = "00000000000000000000000000000003";
                break;
            case "4":
                customerId = "00000000000000000000000000000004";
                break;
        }
        return customerId;
    }

    public void importCustomer() {
        Resource resource = loadResource("customer-总结.xlsx");
        try {

            File file = resource.getFile();
            String[][] data = PoiUtils.getFromExcel(15, file);
            List<Customer> customerList = new ArrayList<>();
            if (data != null && data.length > 0) {
                customerService.deleteAll();
                customerTransGroupService.deleteAll();
                for (int i = 1; i < data.length; i++) {
                    String[] d = data[i];
                    Customer customer = new Customer();
                    customer.setSaasNo(d[0]);
                    customer.setName(d[1]);
                    customer.setNo(d[2]);
                    customer.setOldNo(d[3]);
                    customer.setShortName(d[4]);
                    customer.setShortNo(d[5]);
                    customer.setCustomerTypeId(getCustomerId(d[6]));
                    customer.setWorkGroup(d[7]);
                    String transGroupName = d[8];
                    CustomerTransGroup dbCustomerTransGroup = customerTransGroupService.findByName(transGroupName);
                    if (dbCustomerTransGroup == null) {
                        dbCustomerTransGroup = new CustomerTransGroup();
                        dbCustomerTransGroup.setName(transGroupName);
                        dbCustomerTransGroup = customerTransGroupService.save(dbCustomerTransGroup);
                        dbCustomerTransGroup.setNo(null);//todo
                    }
                    customer.setCustomerTransGroupId(dbCustomerTransGroup.getId());
                    customer.setProvince(d[9]);
                    customer.setCity(d[10]);
                    customer.setArea(d[11]);
                    customer.setAddress(d[12]);
                    customer.setXaxis(new BigDecimal(d[13]));
                    customer.setYaxis(new BigDecimal(d[14]));
                    customerList.add(customer);
                }
                customerService.save(customerList);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void importMaterial() {
        Resource resource = loadResource("material-汇总.xlsx");
        try {
            File file = resource.getFile();
            String[][] data = PoiUtils.getFromExcel(12, file);
            List<MaterialBase> materialBaseList = new ArrayList<>();
            if (data != null && data.length > 0) {
                materialBaseService.deleteAll();
                for (int i = 2; i < data.length; i++) {
                    String[] d = data[i];
                    MaterialBase materialBase = new MaterialBase();
                    materialBase.setName(d[0]);
                    materialBase.setNo(d[1]);
                    materialBase.setOldNo(d[2]);
                    materialBase.setMaterialTypeId(d[9]);
                    materialBase.setMaterialUnitId(d[10]);
                    materialBaseList.add(materialBase);
                }
                materialBaseService.save(materialBaseList);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void importNutrition() {
        Resource resource = loadResource("nutrition-总结.xlsx");
        try {
            File file = resource.getFile();
            String[][] data = PoiUtils.getFromExcel(5, file);
            List<Nutrition> nutritionList = new ArrayList<>();
            if (data != null && data.length > 0) {
                nutritionService.deleteAll();
                nutritionTypeService.deleteAll();
                for (int i = 2; i < data.length; i++) {
                    String[] d = data[i];
                    Nutrition nutrition = new Nutrition();
                    MaterialBase materialBase = materialBaseService.findByNo(d[0]);
                    if (materialBase != null) {
                        nutrition.setMaterialBaseId(materialBase.getId());
                    }
                    NutritionType nutritionType = nutritionTypeService.findByName(d[2]);
                    if (nutritionType == null) {
                        nutritionType = new NutritionType();
                        nutritionType.setName(d[2]);
                        nutritionType = nutritionTypeService.save(nutritionType);
                    }
                    nutrition.setNutritionTypeId(nutritionType.getId());
                    nutrition.setContent(converContent(d[3], d[4]));
                    nutritionList.add(nutrition);
                }
                nutritionService.save(nutritionList);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        System.out.println(ProjectUtil.getBigDecimalVal("1.98 "));
    }

    /**
     * 含量换算成 (mg)/500g
     * content 单位 : 营养成分含量/100g
     * @param content
     * @param unit
     * @return
     */
    private BigDecimal converContent(String content, String unit) {
        BigDecimal contentVal = BigDecimal.ZERO;
        if (!"一".equals(content)) {
            System.out.println("=================:" + content);
            contentVal = ProjectUtil.getBigDecimalVal(content);
        }
        switch (unit) {
            //KJ 不知道怎么换算
            case "KJ":
                break;
            case "g":
                contentVal = contentVal.multiply(new BigDecimal(5000));
                break;
            case "mg":
                contentVal = contentVal.multiply(new BigDecimal(5));
                break;
        }
        return contentVal;
    }

    private Resource loadResource(String filename) {
        return loader.getResource("classpath:init_excel/" + filename);
    }

}
